package com.jd.fruit.controller.mobile;

import java.io.IOException;

import com.jd.fruit.common.annotation.Controller;
import com.jd.fruit.common.controller.BaseController;
import com.jd.fruit.common.util.DateUtils;
import com.jd.fruit.common.util.MD5;
import com.jd.fruit.common.util.StringUtil;
import com.jd.fruit.model.admin.order.OrderUser;

@Controller("/mobile/user")
public class MobileUserController extends BaseController {
	public static final String BUY_INDENTY = "2";//买家

	public void login() {
		render("/mobile/user/login.html");
	}

	public void doLogin() {
			String userName = (String) (getPara("iphone_no") == null ? "": getPara("iphone_no"));
			String password = (String) (getPara("iphone_password") == null ? "": getPara("iphone_password"));
			password = MD5.md5(password);
			//登陆
			OrderUser loginUser = OrderUser.dao.checkLogin(userName, password);
			if(loginUser!=null){
				this.setSessionAttr("loginId", loginUser.getStr("id"));
				this.setSessionAttr("loginUser", loginUser);
				renderText("ok");
				//redirect("admin_login.html");
			}else{
				setAttr("msg", "验证码输入错误,登录失败！");
				redirect("/mobile/user/admin_login_error.html");
			}

	}
	/**
	 * 手机号码检查
	 * 
	 */
	public void checkTelphone() throws IOException {
		//手机号
		String telphone = this.getPara("userName");
		Boolean result=false;
		result=OrderUser.dao.checkTelphone(telphone);
		if(result==true){
			this.renderText(result.toString());
		}else{
			this.renderText(result.toString());
		}
	}
	/**
	 * 用户注册实现方法
	 */
	public void register() {
		render("/mobile/user/register.html");
	}
	
	@SuppressWarnings("static-access")
	public void doRegister() {
		OrderUser user = getModel(OrderUser.class);
		String iphone_no=getPara("iphone_no")== null ? "":getPara("iphone_no");
		user.set("iphone_no", iphone_no);
		String password = (String) (getPara("iphone_password") == null ? "": getPara("iphone_password"));
		//密码设定加密
		password = MD5.md5(password);
		user.set("iphone_password", password);
		//注册时间
		DateUtils date=new DateUtils();
		user.set("register_time",date.getNowTime());
		//买家  
		user.set("identity",BUY_INDENTY);
		//主键
		user.set("id", StringUtil.getKey());
		if(iphone_no!=null && password!=null){
			OrderUser.dao.register(user);
			getRequest().setAttribute("id", user.get("id"));
			render("/mobile/user/login.html");
		}
		else{
			getRequest().setAttribute("id", user.get("id"));
			render("/mobile/user/register_error.html");
		}


	}
}
